package com.best.onegous.details;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.BaseActivity;
import com.best.onegous.R;
import com.best.onegous.Rewrite.EditTextWithDel;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.http.BusinessResponse;
import com.best.onegous.http.HttpClients;
import com.loopj.android.http.RequestParams;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LoGo extends  BaseActivity implements OnClickListener,BusinessResponse{
	private LinearLayout rl_user;
	private Intent intent;
	private EditTextWithDel name,pass;
	private HttpClients httpclient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setC(R.layout.logo_activity);
		rl_user=(LinearLayout) findViewById(R.id.rl_user);
		findViewById(R.id.register).setOnClickListener(this);
		Animation anim=AnimationUtils.loadAnimation(this, R.anim.login_anim);
		anim.setFillAfter(true);
		rl_user.startAnimation(anim);
		
		name = (EditTextWithDel) findViewById(R.id.name);
		pass = (EditTextWithDel) findViewById(R.id.pass);
		findViewById(R.id.logo).setOnClickListener(this);
		httpclient = new HttpClients(this);
		httpclient.addResponseListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
		switch (v.getId()) {
		case R.id.register:
			intent = new Intent(this,Register.class);
			startActivity(intent);
			break;
		case R.id.logo:
			String namestr = name.getText().toString();
			String passstr = pass.getText().toString();
			if(!"".equals(namestr)){
				if(!"".equals(passstr)){
					  RequestParams params = new RequestParams();
					  params.add("username",namestr);
					  params.add("password",passstr);
					httpclient.httpclient(ApiInterface.LOGO, params, true);
				}else{
					toast("密码不能为空！");
				}
			}else{
				toast("用户名不能为空！");
			}
			
			break;

		}
	}
	@Override
	public void OnMessageResponse(String url, JSONObject jo)
			throws JSONException {
		if (null != jo) {
			if (url.equals(ApiInterface.LOGO)) {
				toast("登录成功！");
				JSONObject data = jo.getJSONObject("data");
				String code_idstr = data.getString("code_id");
				String user_idstr = data.getString("user_id");
				
				Apps.editor.putString("code_id", code_idstr);
				Apps.editor.putString("user_id", user_idstr);
				Apps.editor.commit();
				Common.code_id = code_idstr;
				Common.user_id =user_idstr;
				finish();
				overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
			}
			
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	 // TODO Auto-generated method stub
	 if(keyCode == KeyEvent.KEYCODE_BACK)
		{ 
		 finish();
		overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
		}
	 return false;
	}
}
